WriterDetailPageManager=function(){
	this.inited=false;
	this.writerId=null;
	this.mainScroll=null;
	this.options={},
	this.writerAppVM=null;
	this.shiwenListLoader=null;
}
 //
WriterDetailPageManager.defValue={
	ilikeItBtnEl:'#i-like-it',
	ilikeItFlagClass:'i-like-it-flag-class',
	toWriterDetailPageSettingEl:'#writer-det-page-setting',
	writerAttrListUl:"#writer-attr-list-ul",
	writerVMConfig:{
		el:"#xq-writer-detail-card",
		data:{
			id:null,
			name:"",
			shiwenCount:0,
			hotLevel:"未知",
			attrCount:0,
			coverImg:"../images/writer-detail-bg.jpg",
			headImageUrl:"../images/defWriterHeader001.jpg",
			dynasty:"",
			simpleIntro:"",
			attrList:[]
		}
	},
	mainScrollConfig:{
	   el:"#xq-writer-detail-scroll-wrapper",
	   up:{
			loadFunction:function(manager){
				manager.loadShiwenList();
			}
	   },
	   scrollCallBack:function(that,e){
		   var $writerHeaderBg=$('.writer-header-bg');
		   var bgHeight = $writerHeaderBg.outerHeight();
		   var scrollTop = that.scroll.y;
		   if(scrollTop <= (0-bgHeight)){
			   if($('#fixed-writer-content').is('.xq-hidden')){
				   console.log(scrollTop);
				   var writerInfoClone=$('.xq-writer-detail-content').find('.writer-header-content').clone(true);
				   $('#fixed-writer-content').empty().append(writerInfoClone);
				   $('#fixed-writer-content').removeClass('xq-hidden');
				   setTimeout(function(){
					  $('#fixed-writer-content').find('img').addClass('smaller-header-img'); 
				   },50); 
			   }
		   }else if(scrollTop>(0-bgHeight)){
			   if(!$('#fixed-writer-content').is('.xq-hidden')){
			   		$('#fixed-writer-content').empty();
			   		$('#fixed-writer-content').addClass('xq-hidden');
			   }
		   }
	   }
	},
	shiwenListLoaderConfig:{
		appVMConfig:{el:'#writer-shiwen-list-ul'},
		itemContentEl:"#writer-shiwen-list-ul",
		loadParam:{}
	}
}

WriterDetailPageManager.prototype.init=function(opts){
	if(!this.inited){
		var that=this;
		this.options=$.extend(true,{},WriterDetailPageManager.defValue,opts);
		this.writerId=this.options.writerId;
		//
		this.loadWriterDetailInfo(this.writerId);
		this.writerAppVM=new Vue(this.options.writerVMConfig);
		//
		this.shiwenListLoader=new ShiwenListLoader();
		this.options.shiwenListLoaderConfig.loadOverCallback=function(loader,data){
			that.mainScroll.loadOver(data.entry.hasNextPage);
		}
		this.options.shiwenListLoaderConfig.loadParam['writerId']=this.writerId;
		this.shiwenListLoader.init(this.options.shiwenListLoaderConfig);
		//
		var mainScrollConfig=this.options.mainScrollConfig;
		var configUpFunction=mainScrollConfig.up.loadFunction;
		mainScrollConfig.up.loadFunction=function(){
			configUpFunction(that);
		}
		this.mainScroll=new RefreshAbleScroll(mainScrollConfig.el,mainScrollConfig);
		//
		this.resizeWindow();
		this.registerEvent();
	}
	this.inited=true;
}

WriterDetailPageManager.prototype.loadWriterDetailInfo=function(writerId){
	var that=this;
	writerId=writerId||this.writerId;
	if(writerId){
		this.initILikeIt(writerId);
		var selVal = SettingInfoManager.getWriterDetPageCoverTypeSetting();
		var requestVersion=getRequestVersion();
		var requestData={id:writerId,version:requestVersion};
		if(selVal){
			requestData.coverShowType=selVal;
		}
		var writerVMConfig=that.options.writerVMConfig;
		var url=getShiciServiceHost()+'/shici/writer/detail';
		muiAjaxGetJsonData({
			showLoading:true,
			url:url,
			data:requestData,
			callback:function(data){
				if(!data.entry){
					return;
				}
				SettingInfoManager.addHistoryInfo(SettingInfoManager.settingName.historyWriter,writerId);
				var defValue=WriterDetailPageManager.defValue.writerVMConfig;
				$.extend(true,writerVMConfig.data,defValue,data.entry);
			},
		});
	}
}

WriterDetailPageManager.prototype.loadShiwenList=function(){
	var loadParam=this.shiwenListLoader.getLoadParam();
	this.shiwenListLoader.loadShiwenList();
	loadParam.pageNo++;
}

WriterDetailPageManager.prototype.resizeWindow=function(){
	var winH=$(window).height();
	var headerH=$('.xq-window-header').height();
	var bodyH=winH-headerH-1;
	//$('.xq-window-body').css('padding-bottom',bottomPadding+'px');
	$('.xq-window-body').css('min-height',winH+'px');
}

WriterDetailPageManager.prototype.registerEvent=function(){
	var that=this;
	var writerVMConfig=this.options.writerVMConfig;
	$(that.options.writerAttrListUl).on('tap','li',function(e){
		var index=$(this).closest('li').attr('index');
		var attrInfo=writerVMConfig.data.attrList[index];
		var infoId=attrInfo["id"];
		var infoTitle=writerVMConfig.data["name"];
		console.info(infoId+":"+infoTitle);
		mui.openWindow({
			url:'writerAttrValue.html?infoId='+infoId,
			id:'writerAttrValue.html-infoId-'+infoId,
			extras:{
				infoId : infoId,
				infoTitle : infoTitle
			}
		});
	});
	$(that.options.toWriterDetailPageSettingEl).on('tap',function(e){
		mui.openWindow({
			url:'settingOfWriterDetai.html',
			id:'settingOfWriterDetai.html'
		});
	});
	//
	$(this.options.ilikeItBtnEl).on('tap',function(e){
		that.toogleILikeIt();
	});
}
				
WriterDetailPageManager.prototype.initILikeIt=function(writerId){
	writerId=writerId||this.writerId;
	if(writerId){
		var list=SettingInfoManager.getList(SettingInfoManager.settingName.likeWriter);
		var index=$.inArray(writerId+'', list);
		var flagClass=this.options.ilikeItFlagClass;
		$(this.options.ilikeItBtnEl).removeClass(flagClass);
		if(index>=0){
			$(this.options.ilikeItBtnEl).addClass(flagClass);
		}
	}
}
//
WriterDetailPageManager.prototype.toogleILikeIt=function(){
	var writerId=this.writerId;
	if(writerId){
		var flagClass=this.options.ilikeItFlagClass;
		if($(this.options.ilikeItBtnEl).is('.'+flagClass)){
			SettingInfoManager.removeFromList(SettingInfoManager.settingName.likeWriter,writerId);
			$(this.options.ilikeItBtnEl).removeClass(flagClass);
		}else{
			SettingInfoManager.addToList(SettingInfoManager.settingName.likeWriter,writerId,false);
			$(this.options.ilikeItBtnEl).addClass(flagClass);
		}
	}
}